package cn.jiedanba.cacert.caadmin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.jiedanba.cacert.admin.vo.page.PageDomain;
import cn.jiedanba.cacert.admin.vo.page.TableDataInfo;
import cn.jiedanba.cacert.admin.vo.page.TableSupport;
import cn.jiedanba.cacert.common.util.ServletUtils;

public abstract class BaseController {

	protected HttpServletRequest getRequest() {
		return ServletUtils.getRequest();
	}

	protected HttpServletResponse getResponse() {
		return ServletUtils.getResponse();
	}

	/**
	 * 设置请求分页数据
	 */
	protected void startPage() {
		PageDomain pageDomain = TableSupport.buildPageRequest(ServletUtils.getRequest());
		Integer pageNum = pageDomain.getPageNum();
		Integer pageSize = pageDomain.getPageSize();
		if (pageNum != null && pageSize != null) {
			PageHelper.startPage(pageNum, pageSize, pageDomain.getOrderBy());
		}
	}

	/**
	 * 响应请求分页数据
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected TableDataInfo getDataTable(List<?> list) {
		TableDataInfo rspData = new TableDataInfo();
		rspData.setCode(0);
		rspData.setRows(list);
		rspData.setTotal(new PageInfo(list).getTotal());
		return rspData;
	}
}
